import { QueryClient, useMutation } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { TestService } from "@/shared/api/api.test";

export function useCheckMenyTests() {
    const queryClient = new QueryClient
    const checkTestMutation = useMutation(
        {
            mutationFn: TestService.checkManyTests,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const errorMessage = checkTestMutation.error?.response?.data.message

    return {
        errorMessage,
        isSuccess: checkTestMutation.isSuccess,
        response: checkTestMutation.data,
        handleSubmit: (tests: { testId: number, answers: { question: string, answer: string }[] }[]) => {
            checkTestMutation.mutate(tests)
        }
    };
}